package co.edu.uniandes.webdesktop.chat.client.interfaz;

import co.edu.uniandes.webdesktop.chat.client.mundo.Amigo;
import co.edu.uniandes.webdesktop.chat.client.mundo.login.LoginService;
import co.edu.uniandes.webdesktop.chat.client.mundo.login.LoginServiceAsync;

import com.extjs.gxt.ui.client.widget.Window;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;


public class VentanaLogin extends Window
{
	
	// Textbox para el Login
	TextBox txtLogin;
	
	// PasswordTextBox para el Password
	PasswordTextBox txtPass;

	
	public VentanaLogin()
	{
		RootPanel rootPanel = RootPanel.get();
		{
			txtLogin = new TextBox();
			rootPanel.add(txtLogin, 181, 58);
			txtLogin.setSize("129px", "18px");
		}
		{
			Label label = new Label("Login");
			rootPanel.add(label, 90, 58);
		}
		{
			Label label = new Label("Password");
			rootPanel.add(label, 81, 97);
		}
		{
			txtPass = new PasswordTextBox();
			rootPanel.add(txtPass, 181, 97);
			txtPass.setSize("129px", "18px");
		}
		{
			Button button = new Button("Conectar");
			button.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) 
				{
					String login = txtLogin.getValue();
					String password = txtPass.getValue();
					validar( login, password);
					
				}

			});
			rootPanel.add(button, 245, 130);
		}

	}
	private void validar(String login, String password) {
		// TODO Auto-generated method stub

		System.out.println("VA A INTENTAR VALIDAR");
	      LoginServiceAsync msg = GWT.create(LoginService.class);

		    // Set up the callback object.
		    AsyncCallback<Amigo[]> callback = new AsyncCallback<Amigo[]>() {
		      public void onFailure(Throwable caught) {
		    	  System.out.println("ERROR!!");
		        // TODO: Do something with errors.
		      }

			@Override
			public void onSuccess(Amigo[] result) {
				// TODO Auto-generated method stub
				refreshPanelAmigos(result);
			}
		    };

		    // Make the call to the stock price service.
		    msg.autenticar(login, password, callback);
		    System.out.println("PASO LA VALIDACION!!");
		
	}
	protected void refreshPanelAmigos(Amigo[] result) {
		// TODO Auto-generated method stub
		
	}
}
